package com.example.demo.util;

import com.alibaba.fastjson.JSONObject;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.model.HttpRequestBody;
import us.codecraft.webmagic.utils.HttpConstant;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Created by 陈淦彬
 * @date 2018/10/16
 */
public class util {
    private final static AtomicInteger pageNo = new AtomicInteger(1);

    private final static String url = "http://wap.gd.10086.cn/nwap/card/niceNumber/changePage.jsps";

    public static Request getRequest() {

        Request request = new Request(url);
        request.setMethod(HttpConstant.Method.POST);

        Map<String, Object> map = new HashMap<>();
        map.put("areaSId",2);
        map.put("city",663);
        map.put("pageNo",pageNo.get());

        request.setRequestBody(HttpRequestBody.form(map,"utf-8"));

        request.addHeader("Connection", "keep-alive");
        request.addHeader("Content-Type", "application/x-www-form-urlencoded");
        request.addHeader("Referer", "http://wap.gd.10086.cn/nwap/card/niceNumber/index.jsps?city=663&t=20181014210912960");
        request.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0");
        request.addHeader("Host", "wap.gd.10086.cn");
        request.addHeader("Accept", "*/*");
        request.addHeader("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
        request.addHeader("Accept-Encoding", "gzip, deflate");
        request.addHeader("X-Requested-With", "XMLHttpRequest");
        request.addHeader("Connection", "keep-alive");
        request.addHeader("Pragma", "no-cache");
        request.addHeader("Cache-Control", "no-cache");
        request.addHeader("Content-Language", "zh-CN");
        request.addHeader("Cookie", "hibext_instdsigdipv2=1; WT_FPC=id=23471bce22b1fe3c6771539669927922:lv=1539669928698:ss=1539669927922; JSESSIONID=0000740cSXOHzD93mef_xVoQev9:1b40u14d6");
        request.addHeader("pageNo", String.valueOf(pageNo.get()));

        pageNo.getAndIncrement();

        /*
        Cookie: JSESSIONID=0000dZ4eqYUXdY0x577e_0vSctt:1b40u143e; hibext_instdsigdipv2=1
         */

        return request;
    }
}
